package com.ynhenc.pta.routeRank.impl;

import java.util.ArrayList;

import com.mappers.jrps.dto.*;
import com.mappers.jrps.second.*;

import com.ynhenc.comm.NetAddr;
import com.ynhenc.pta.routeRank.*;

public abstract class SectionListVehicle extends ComLib_PTA implements SectionListGetter {

	public static final double KmPerHour = 5.0 / 18.0;

	public SectionListVehicle(TransitType transitType) {
		this.transitType = transitType;
		if (this.transitType == null) {
			this.transitType = TransitType.AUTOMOBILE;
		}
	}

	abstract public int getOptionRoute();

	abstract public double getVelocity_MeterPerSec();

	final public TransitType getTransitType() {
		return this.transitType;
	}

	final public String getSectionListGetterName(StartEndNode seNode) {
		String sName = this.getTransitType().getTransitName();

		if (seNode != null) {
			sName += "(" + seNode.getStartNode().getName() + " : " + seNode.getEndNode().getName() + ")";
		}
		return sName;
	}

	public SectionList getSectionList(StartEndNode seNode) throws Exception {

		DurationChecker durationChecker = new DurationChecker();

		durationChecker.checkNow();

		SectionList sectionList = new SectionList();

		PathImpl path = new PathImpl();

		Section section = new SectionImpl(seNode, path);

		// 항법 서버 주소 설정.
		NetAddr netAddr = RouteRequestImpl.getNaviServerAddr();
		if (netAddr == null) {
			netAddr = SysConfPta.NET_ADDR_01_NAVI;
		}
		this.debug("Navi Server:" + netAddr);
		// 끝. 항법 서버 주소 설정.

		Request req = new Request();

		req.setVersion(2);
		req.setReqType(0);

		int coordType = 0; // 0 : WGS84, 1 : KATEC
		req.setCoordType(coordType);

		int optionRoute = this.getOptionRoute();

		this.debug("optionRoute:" + optionRoute);

		req.setOptionRoute1(optionRoute);

		int carType = 0;
		req.setCarType(carType);

		final double convFactor = 256 * 2048;

		double startX = (127 + (1 / 60.0)) * convFactor;
		double startY = (37 + (29 / 60.0)) * convFactor;
		double endX = (126 + (58 / 60.0)) * convFactor;
		double endY = (37 + (33 / 60.0)) * convFactor;

		Node sNode = null;
		Node eNode = null;

		if (seNode == null) {
			this.debug("Wring StartEndNode!!!!");
		} else {
			sNode = seNode.getStartNode();
			eNode = seNode.getEndNode();

			System.out.println("Node Start:" + sNode);
			System.out.println("Node End  :" + eNode);

			if (sNode == null || eNode == null) {
				this.debug("Wring start node or end node!!!!");
			} else {
				startX = sNode.getX() * convFactor;
				startY = sNode.getY() * convFactor;
				endX = eNode.getX() * convFactor;
				endY = eNode.getY() * convFactor;
			}
		}

		req.getStart().setX((int) startX);
		req.getStart().setY((int) startY);
		req.getEnd().setX((int) endX);
		req.getEnd().setY((int) endY);

		try {

			Connection conn = new Connection();
			conn.connect(netAddr.getHostName(), netAddr.getPortNo());
			conn.setRequest(req);
			Response res = conn.searchRoute();

			// set totoal time and totoal length
			Summary summary = res.getSummary();
			double length_Meter = summary.getTotalLength(0);
			section.setLength_Meter(length_Meter);
			
			double velocity_MeterPerSec = this.getVelocity_MeterPerSec();
			double time_Sec = 0;
			if (this.getTransitType() == TransitType.AUTOMOBILE) {
				time_Sec = summary.getRemainTime(0);
				section.setTime_Sec(time_Sec);
			} else {
				time_Sec = length_Meter / velocity_MeterPerSec;
				section.setTime_Sec(time_Sec);
			}
			this.debug("time: " + time_Sec + ", lenght: " + length_Meter + " (m)");
			// end of setting total time and length

			if (sNode != null) { // add start node on force
				path.addVertex(sNode);
			} // end of adding start node on force

			ArrayList vertexList = res.getVertexList(0);

			Point p;
			double wgsX, wgsY;
			for (int i = 0, iLen = vertexList.size(); i < iLen; i++) {
				p = (Point) vertexList.get(i);
				wgsX = ((p.getX() + 0.0) / convFactor);
				wgsY = ((p.getY() + 0.0) / convFactor);
				path.addVertex(wgsX, wgsY);
				if (false) {
					System.out.println("index(" + i + ")" + " x =" + p.getX() + ", y=" + p.getY() + " conv : " + wgsX + ", " + wgsY);
				}
			}

			if (eNode != null) { // add end node on force
				path.addVertex(eNode);
			} // end of adding end node on force

			conn.close();

		} catch (Exception e) {
			// this.debug(e);
		}

		durationChecker.checkNow();
		section.setQueryTime_Sec(durationChecker.getDuration());

		sectionList.add(section);

		return sectionList;
	}

	private TransitType transitType = TransitType.AUTOMOBILE;
}
